<html>
<head>
  <title>03pandas</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="503"/>
<h1>03pandas</h1>

<div>
<span><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">Pandas最初被作为金融数据分析工具而开发出来，因此，pandas为</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">时间序列分析提供了很好的支持。</span></div><div><br/></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135); font-weight: bold;">基本功能</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">开发pandas时提出的需求</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">具备按轴自动或显式数据对齐功能的数据结构</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">集成时间序列功能</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">既能处理时间序列数据也能处理非时间序列数据的数据结构</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">数学运算和约简（比如对某个轴求和）可以根据不同的元数据（轴编号）执行</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">灵活处理缺失数据</span></div><div><span style="font-size: 14pt; color: rgb(28, 51, 135);">•</span> <span style="font-size: 14pt; color: rgb(28, 51, 135);">合并及其他出现在常见数据库（例如基于SQL的）中的关系型运算</span></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: bold;">数据结构 Series</span></font></div><div>• Series是一种类似于一维数组的对象，它由一组数据（各种NumPy数据类型）</div><div>以及一组与之相关的数据标签（即索引）组成。</div><div>• Series的字符串表现形式为：索引在左边，值在右边。</div><div>• 创建</div><div>• 读写</div><div>• 运算</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">from pandas import Series</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">用数组生成Series</span><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">')</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">obj = Series([4, 7, -5, 3])</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj.values)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj.index)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print()</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">指定Series的index</span><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">')</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">obj2 = Series([4, 7, -5, 3], index = ['d', 'b', 'a','c'])</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj2)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj2.index)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj2['a'])</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">obj2['d'] = 6</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print (obj2[['c', 'a', 'd']])</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj2[obj2 &gt; 0])</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print('b' in obj2)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print('e' in obj2)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print()</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">使用字典生成Series</span><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">sdata = {'Ohio':45000, 'Texas':71000, 'Oregon':16000, 'Utah':5000}</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">obj3 = Series(sdata)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print(obj3)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">print()</span></div></div><div><img src="03pandas_files/Image.png" type="image/png" data-filename="Image.png"/></div><div><img src="03pandas_files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><img src="03pandas_files/Image [2].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: bold;">数据结构 DataFrame</span></font></div><div>• DataFrame是一个表格型的数据结构，它含有一组有序的列，每列可以是不同</div><div>的值类型（数值、字符串、布尔值等）。</div><div>• DataFrame既有行索引也有列索引，它可以被看做由Series组成的字典（共用</div><div>同一个索引）。</div><div><br/></div><div><span style="min-height: 26pt; font-size: 20pt; color: rgb(1, 1, 1); font-family: ArialMT;">•</span> <span style="min-height: 26pt; font-size: 20pt; color: rgb(1, 1, 1); font-family: MicrosoftYaHei;">可以输入给DataFrame构造器的数据</span></div><div><img src="03pandas_files/Image [3].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div>• 创建</div><div>• 读写</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">from pandas import Series, DataFrame</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">用字典生成DataFrame，key为列的名字。</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">data = {'state':['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        'year':[2000, 2001, 2002, 2001, 2002],</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        'pop':[1.5, 1.7, 3.6, 2.4, 2.9]}</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(DataFrame(data))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(DataFrame(data, columns = ['year', 'state', 'pop'])) <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 指定列顺序</span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></span></div></div><div><img src="03pandas_files/Image [4].png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print( '指定索引，在列中指定不存在的列，默认数据用NaN。')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame2 = DataFrame(data, columns = ['year', 'state', 'pop', 'debt'], index = ['one', 'two', 'three', 'four', 'five'])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2['state'])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2.year)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2.ix['three']</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame2['debt'] = 16.5   # <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">修改一整列</span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2)</span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame2.debt = np.arange(5) <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 用numpy数组修改元素</span></span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2)</span></span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></span></span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div></div><div><img src="03pandas_files/Image [5].png" type="image/png" data-filename="Image.png"/></div><div><img src="03pandas_files/Image [6].png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('用Series指定要修改的索引及其对应的值，没有指定的默认数据用NaN。')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">val = Series([-1.2, -1.5, -1.7], index = ['two', 'four', 'five'])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame2['debt'] = val</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print() </span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('赋值给新列')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame2['eastern'] = (frame2.state == 'Ohio') <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 如果state等于Ohio为True</span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2)</span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame2.columns)</span></span></div><div><span style="font-size: 9pt;"><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></span></div></div><div><img src="03pandas_files/Image [7].png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('DataFrame转置')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">pop = {'Nevada':{2001:2.4, 2002:2.9}, 'Ohio':{2000: 1.5, 2001:1.7, 2002:3.6}}</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame3 = DataFrame(pop)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame3)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(frame3.T)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('指定索引顺序，以及使用切片初始化数据。')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(DataFrame(pop, index = [2001, 2002, 2003])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">pdata = {'Ohio': frame3['Ohio'][: -1], 'Nevada': frame3['Nevada'][:2]}</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(DataFrame(pdata))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(0</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print( '指定索引和列的名称')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame3.index.name = 'year'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame3.columns.name = 'state'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (frame3)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print( frame3.values)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (frame2.values)</span></div></div><div><img src="03pandas_files/Image [8].png" type="image/png" data-filename="Image.png"/></div><div><img src="03pandas_files/Image [9].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">数据结构 索引对象</span></font></div><div><br/></div><div>• pandas的索引对象负责管理轴标签和其他元数据（比如轴名称等）。构建</div><div>Series或DataFrame时，所用到的任何数组或其他序列的标签都会被转换成一<span style="line-height: 1.45;">个Index。</span></div><div>• Index对象是不可修改的（immutable），因此用户不能对其进行修改。不可<span style="line-height: 1.45;">修改性非常重要，因为这样才能使Index对象在多个数据结构之间安全共享。</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import numpy as np</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import pandas as pd</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import sys</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">from pandas import Series, DataFrame, Index</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('获取index')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">obj = Series(range(3), index = ['a', 'b', 'c'])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">index = obj.index</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (index[1:])</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('使用Index对象')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">index = Index(np.arange(3))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">obj2 = Series([1.5, -2.5, 0], index = index)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (obj2)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (obj2.index is index)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('判断列和索引是否存在')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">pop = {'Nevada':{20001:2.4, 2002:2.9},</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        'Ohio':{2000:1.5, 2001:1.7, 2002:3.6}}</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">frame3 = DataFrame(pop)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print( 'Ohio' in frame3.columns)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('2003' in frame3.index)</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div></div><div><img src="03pandas_files/Image [10].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div>• pandas中主要的index对象</div><div><img src="03pandas_files/Image [11].png" type="image/png" data-filename="Image.png"/></div><div>• Index的方法和属性 </div><div><img src="03pandas_files/Image [12].png" type="image/png" data-filename="Image.png"/></div><div><img src="03pandas_files/Image [13].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><div><br/></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 266px;"></col><col style="width: 414px;"></col><col style="width: 555px;"></col></colgroup><tbody><tr><td style="width: 266px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>df.loc(x, y)</div></td><td style="width: 414px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>遍历整个df，并判断X轴是否符合x条件，Y轴是否符合y条件</div><div>以此原理产生loc定位，并可以对定位元素重新赋值</div></td><td style="width: 555px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div><br/></div></td></tr><tr><td style="width: 266px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>df[&quot;x&quot;]=df[&quot;x&quot;].map({string1:int1; string2:int2}). astype(int)</div></td><td style="width: 414px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>将一个feature的value『只有几个现有值』从string转化为int</div></td><td style="width: 555px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div><br/></div></td></tr><tr><td style="width: 266px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>df[&quot;x&quot;]=df[&quot;x&quot;].replace([s1,s2],ss)</div></td><td style="width: 414px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>将包含的s1,s2 全部替换成ss</div></td><td style="width: 555px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div><br/></div></td></tr><tr><td style="width: 266px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>df = df.drop(drop_elements,axis=1)</div></td><td style="width: 414px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>将一整列舍弃掉</div></td><td style="width: 555px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div><br/></div></td></tr><tr><td style="width: 266px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>n = df.shape[0]</div></td><td style="width: 414px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div>返回df的行数（items的数量）</div></td><td style="width: 555px; padding: 8px; border: 1px solid rgb(204, 204, 204);"><div><br/></div></td></tr></tbody></table><div><br/></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></span>
</div></body></html> 